﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuaCheng.DataAccess
{
    public class IndexPageHelper
    {
        /// <summary>
        /// 新增设备连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="devtype"></param>
        public static void ModifyDevices(string ip,string devtype)
        {
            try
            {
                string sql = "select dev_ip from devices where dev_ip=@ip and dev_type=@type";
                DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@type", devtype));
                var dt = DBAccessManager.SqlDBAccess.ExecuteScalar(cmd);
                if (dt==null)//如果记录不存在则插入数据库
                {
                    try
                    {
                        string secSql = "insert  into devices(dev_ip,dev_type,dev_delete) values(@newip,@newtype,@del)";
                        DbCommand secCmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, secSql);
                        secCmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@newip", ip));
                        secCmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@newtype", devtype));
                        secCmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@del", false));
                        DBAccessManager.SqlDBAccess.ExecuteNonQuery(secCmd);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }
    }
}
